package com.hh.vrserver.service.impl;

import com.hh.vrserver.model.VrEntity;
import com.hh.vrserver.model.VrEntityRepostory;
import com.hh.vrserver.service.VrService;
import com.hh.vrserver.web.vo.PageQuery;
import com.hh.vrserver.web.vo.VrData;
import com.hh.vrserver.web.vo.VrDataVo;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDate;
import java.util.List;

/**
 * @author yeweijun
 * @version 1.0 2023/7/5 09:55
 */
@Service
@RequiredArgsConstructor
public class VrServiceImpl implements VrService {
    private final VrEntityRepostory vrEntityRepostory;

    @Override
    public VrData query(String keyId) {
        VrEntity vrEntity = queryByKey(keyId);
        return toOut(vrEntity);
    }

    private VrData toOut(VrEntity vrEntity) {

        return new VrData(vrEntity.getKeyId(), vrEntity.getEncrypted(), vrEntity.getValidStart(), vrEntity.getValidEnd(), "");
    }

    private VrEntity queryByKey(String key) {
        VrEntity vrEntity = vrEntityRepostory.queryVrEntityByKeyId(key);
        if (vrEntity == null) {
            throw new IllegalArgumentException("找不到对应的数据");
        }
        return vrEntity;
    }

    @Override
    public VrData login(String keyId, String password) {
        VrEntity vrEntity = queryByKey(keyId);
        if (!vrEntity.checkPassword(password)) {
            throw new IllegalArgumentException("密码错误");
        }
        return toOut(vrEntity);
    }

    @Override
    public VrData webquery(String keyId) {
        VrEntity vrEntity = queryByKey(keyId);
        return new VrData(vrEntity.getKeyId(), vrEntity.getEncrypted(), vrEntity.getValidStart(), vrEntity.getValidEnd(), vrEntity.getRemark());
    }

    @Override
    public void saveOrUpdate(VrDataVo vrData) {
        VrEntity vrEntity = vrEntityRepostory.queryVrEntityByKeyId(vrData.getKeyId());
        if ((vrData.getId() != null && vrEntity != null && !vrEntity.getId().equals(vrData.getId())) || (vrData.getId() == null && vrEntity != null)) {
            throw new IllegalStateException("设备ID重复");
        }
        if (vrEntity == null) {
            List<LocalDate> dd = vrData.getDateRange();
            vrEntity = new VrEntity(vrData.getKeyId(), vrData.getEncrypted(), vrData.getPassword(), dd.get(0), dd.get(1), vrData.getRemark());
        } else {
            vrEntity.update(vrData);
        }
        vrEntityRepostory.save(vrEntity);
    }

    @Override
    public Page<VrEntity> pageQuery(PageQuery pageQuery) {
        Pageable pageable = PageRequest.of(pageQuery.getPage() - 1, pageQuery.getSize());
        Specification<VrEntity> specification = new Specification<VrEntity>() {
            @Override
            public Predicate toPredicate(Root<VrEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                if (!StringUtils.isEmpty(pageQuery.getKeys())) {
                    predicate.getExpressions().add(criteriaBuilder.or(criteriaBuilder.equal(root.get("keyId"), pageQuery.getKeys()), criteriaBuilder.like(root.get("remark"), pageQuery.getKeys())));
                }
                return query.where(predicate).orderBy(criteriaBuilder.desc(root.get("id"))).getRestriction();
            }
        };
        Page<VrEntity> page = vrEntityRepostory.findAll(specification, pageable);
        return page;
    }
}
